package com.fire.web.modules.all.controller;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fire.common.mybatis.mvc.wrapper.EntityWrapper;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.entity.BuildingModel;
import com.fire.web.modules.all.request.AddBuildingReq;
import com.fire.web.modules.all.service.IBuildingService;
import com.fire.web.modules.all.utils.TokenUtil;
import com.fire.web.modules.all.utils.WrapperUtil;
import com.fire.web.modules.sso.service.IOAuthService;
import com.fire.web.modules.sys.service.IRoleService;
import com.fire.web.modules.sys.service.IUserRoleService;
import com.fire.web.security.shiro.realm.UserRealm;
import com.fire.web.utils.PageRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/building")
public class BuildingController extends CrudController<IBuildingService, BuildingModel> {
	private static final Logger LOGGER = LoggerFactory.getLogger(BuildingController.class);

	@Autowired
	private IOAuthService oAuthService;
	@Autowired
	private IBuildingService iBuildingService;
	@Autowired
	private IRoleService iRoleService;

	/**
	 * 增加建筑物
	 *
	 * @return
	 */
	@PostMapping("/addBuilding")
	@ResponseBody
	public AjaxResult addBuilding(AddBuildingReq req, HttpServletRequest request, HttpServletResponse response) {
		LOGGER.info("==================新增建筑物开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			String token = TokenUtil.getToken(request, response);
			UserRealm.Principal principalByAccessToken = oAuthService.getPrincipalByAccessToken(token);
			String id = principalByAccessToken.getId();

			ajaxResult = iBuildingService.addBuilding(req,id);
		} catch (Exception e) {
			LOGGER.error("BuildingController.addBuilding().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================新增建筑物结束==================");
		return ajaxResult;
	}

	/**
	 * 更新建筑物
	 *
	 * @return
	 */
	@PostMapping("/updateBuilding")
	@ResponseBody
	public AjaxResult updateBuilding(AddBuildingReq req) {
		LOGGER.info("==================更新建筑物开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			ajaxResult = iBuildingService.updateBuilding(req);
		} catch (Exception e) {
			LOGGER.error("BuildingController.updateBuilding().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================更新建筑物结束==================");
		return ajaxResult;
	}

	/**
	 * 查询建筑物下拉选
	 *
	 * @return
	 */
	@PostMapping("/getBuildingItem")
	@ResponseBody
	public AjaxResult getBuildingItem() {
		LOGGER.info("==================查询建筑物下拉选开始==================");
		AjaxResult ajaxResult;
		try {
			ajaxResult = iBuildingService.getBuildingItem();
		} catch (Exception e) {
			LOGGER.error("BuildingController.getBuildingItem().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================查询建筑物下拉选结束==================");
		return ajaxResult;
	}

	/**
	 * 查询列表
	 *
	 * @return
	 */
	@GetMapping("/queryBuildingList")
	public AjaxResult queryBuildingList(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult ajaxResult;
		try {
			String token = TokenUtil.getToken(request,response);
			UserRealm.Principal principalByAccessToken = oAuthService.getPrincipalByAccessToken(token);
			String username = principalByAccessToken.getUsername();
			String userId = principalByAccessToken.getId();
			//遍历参数获得查询条件
			EntityWrapper ew = WrapperUtil.getEw(params);
			//查询
			Page pageBean = iBuildingService.queryBuildingListMap(PageRequest.getPage(), ew,username,userId);
			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "获取成功", pageBean, "获取成功");
		} catch (Exception e) {
			LOGGER.error("BuildingController.page().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);

		return ajaxResult;
	}

}
